package com.ictology.mei;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class NewsDetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);

        final Intent pushViewIntent = this.getIntent();

        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.actionbar);
        TextView title = (TextView) this.findViewById(R.id.action_bar_title);
        title.setText("新聞詳情");

        final ImageView newsDetail_image = (ImageView) this.findViewById(R.id.newsDetail_image);
        final TextView newsDetail_text = (TextView) this.findViewById(R.id.newsDetail_text);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    Document doc = Jsoup.connect(pushViewIntent.getStringExtra("link")).get();
                    Elements elements = doc.select("div#content-container").get(0).select("p");

                    final StringBuffer newsText = new StringBuffer();
                    final StringBuffer newsImage_link = new StringBuffer();

                    if(doc.select("div#content-container").get(0).select("img").size()>0){
                        newsImage_link.append(String.format("http://www.smes-macao.com%s",doc.select("div#content-container").get(0).select("img").get(0).attr("src")));
                    }

                    for(int i=0; i<elements.size()-2; i++){
                        if(elements.get(i).select("img").size() == 0) {
                            newsText.append(elements.get(i).html().replace("&nbsp;", "").toString() + "\n\n");
                        }
                    }

//                    Bitmap img = Picasso.with(NewsDetailActivity.this).load(newsImage_link.toString()).get();
//                    int width = img.getWidth();
//                    int height = img.getHeight();
//
//                    System.out.println(width);
//                    height = height - (width - newsDetail_image.getWidth());
//                    width = newsDetail_image.getWidth();
//                    System.out.println(height);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            newsDetail_text.append(newsText.toString());
                            if(newsImage_link.length()>0) {
//                                try {
//
//
                                Picasso.with(NewsDetailActivity.this).load(newsImage_link.toString()).fit().centerInside().into(newsDetail_image);
//                                }catch (IOException e){
//
//                                }
                            }else{
                                newsDetail_image.setVisibility(View.GONE);
                            }
                        }
                    });

                }catch(IOException e){

                }
            }
        }).start();




    }
}
